package com.samuel.el;


import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

import java.io.IOException;


/**
 * @date:2017/11/21
 * @time: 20:02
 * @author:zhaoyang
 * @desc:配置类
 **/
@Configuration
@ComponentScan("com.samuel.el")
@PropertySource("classpath:test.properties")
public class ElConfig {


    // 1.字符串
    @Value("I say normal")
    private String normalStr;

    // 2.系统属性
    @Value("#{systemProperties['os.name']}")
    private String normalSys;

    //3.表达式结果
    @Value("#{ T(java.lang.Math).random() * 100.0 }")
    private String normalEL;

    //4.其他类的Filed
    @Value("#{demoService.another}")
    private String otherClassFiled;

    // 5.资源文件
    @Value("classpath:test.txt")
    private Resource normalFile;

    //6.网址
    @Value("http://www.baidu.com")
    private Resource normal;

    //7.属性文件
    @Value("${book.author}")
    private String PropertyFiled;

    //7
    @Autowired
    private Environment environment;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigure(){
        return new PropertySourcesPlaceholderConfigurer();
    }


    public void outputResource() throws IOException {
        System.out.println(normalStr);
        System.out.println("------------------");
        System.out.println(normalSys);
        System.out.println("------------------");
        System.out.println(normalEL);
        System.out.println("------------------");
        System.out.println(otherClassFiled);
        System.out.println("------------------");
        System.out.println(IOUtils.toString(normalFile.getInputStream()));
        System.out.println("------------------");
        System.out.println(IOUtils.toString(normal.getInputStream()));
        System.out.println("------------------");
        System.out.println(PropertyFiled);
        System.out.println("------------------");
        System.out.println(environment.getProperty("book.author"));

    }

}
